﻿using FlyHorse.LIMS.DALFactory;
using FlyHorse.LIMS.IDAL;
using FlyHorse.LIMS.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;

namespace FlyHorse.LIMS.BLL
{
    public class ToolBll
    {
        public IDBSession CurrentDBSession
        {
            get
            {
                // return new DBSession();//暂时
                return DBSessionFactory.CreateDBSession();
            }
        }

        public OJCB4Entities entity = (OJCB4Entities)FlyHorse.LIMS.DAL.DBContextFactory.CreateDbContext();
        /// <summary>
        /// 获取样品类型List
        /// </summary>
        /// <returns></returns>
        public List<GoodsType> GetGoodsTypeList()
        {
            var models = this.CurrentDBSession.GoodsTypeDal.LoadEntitiesAsNoTracking(m => true).ToList();
            return models;
        }

        /// <summary>
        /// 获取取样位置List,不包含已经删除的，一般用来绑定下拉框
        /// </summary>
        /// <returns></returns>
        public List<SamplingPlace> GetSamplingPlaceList()
        {
            var models = this.CurrentDBSession.SamplingPlaceDal.LoadEntitiesAsNoTracking(m => true).OrderBy(m => m.SamplingPlaceID).ToList();
            return models;
        }

        /// <summary>
        /// 获取取样位置List,包含已经删除的，一般用来绑定grid里下拉框
        /// </summary>
        /// <returns></returns>
        public List<SamplingPlace> GetAllSamplingPlaceList()
        {
            var models = this.CurrentDBSession.SamplingPlaceDal.LoadEntitiesAsNoTracking(m => true).OrderBy(m => m.SamplingPlaceID).ToList();
            return models;
        }

        /// <summary>
        /// 获取所有样品的名称的List，包括已经删除的，一般用于绑定grid里的下拉框
        /// </summary>
        /// <returns></returns>
        public List<Good> GetAllGoodList()
        {
            var models = this.CurrentDBSession.GoodDal.LoadEntitiesAsNoTracking(m => true).OrderBy(m=>m.GoodsName).ToList();
            return models;
        }

        /// <summary>
        /// 获取所有样品的名称的List，不包括已经删除的，一般用于绑定搜索条件里的下拉框
        /// </summary>
        /// <returns></returns>
        public List<Good> GetGoodList()
        {
            var models = this.CurrentDBSession.GoodDal.LoadEntitiesAsNoTracking(m => true).OrderBy(m => m.GoodsName).ToList();
            return models;
        }

        /// <summary>
        /// 获取原材料名称的List 不包括已经删除的
        /// </summary>
        /// <returns></returns>
        public List<Good> GetRawGoodList()
        {
            GoodsType GoodsType = this.CurrentDBSession.GoodsTypeDal.LoadEntitiesAsNoTracking(m => m.GoodsTypeName == "原材料").First();
            var models = this.CurrentDBSession.GoodDal.LoadEntitiesAsNoTracking(m => m.GoodsTypeID == GoodsType.GoodsTypeID).OrderBy(m => m.GoodsName).ToList();
            return models;
        }

        /// <summary>
        /// 获取产品名称的List 不包括已经删除的
        /// </summary>
        /// <returns></returns>
        public List<Good> GetSemiGoodList()
        {
            GoodsType GoodsType = this.CurrentDBSession.GoodsTypeDal.LoadEntitiesAsNoTracking(m => m.GoodsTypeName == "产品").First();
            var models = this.CurrentDBSession.GoodDal.LoadEntitiesAsNoTracking(m => true && m.GoodsTypeID == GoodsType.GoodsTypeID).OrderBy(m => m.GoodsName).ToList();
            return models;
        }

        /// <summary>
        /// 获取所有的用户列表，包括已经删除的，一般用于绑定grid
        /// </summary>
        /// <returns></returns>
        public List<LoginUser> GetAllLoginUersList()
        {
            var models = this.CurrentDBSession.LoginUserDal.LoadEntitiesAsNoTracking(m => true).OrderBy(m => m.UserName).ToList();
            return models;
        }


        /// <summary>
        /// 获取报告类型list
        /// </summary>
        /// <returns></returns>
        public List<SysReportType> GetReportTyepList()
        {
            var models = this.CurrentDBSession.SysReportTypeDal.LoadEntitiesAsNoTracking(m => true).ToList();
            return models;
        }

        /// <summary>
        /// 获取检验项目list，不包括已经删除的
        /// </summary>
        /// <returns></returns>
        public List<TestItem> GetTestItemList()
        {
            var models = this.CurrentDBSession.TestItemDal.LoadEntitiesAsNoTracking(m => true).OrderBy(m=>m.TestItemID).ToList();
            return models;
        }

        /// <summary>
        /// 获取检验项目list，不包括已经删除的
        /// </summary>
        /// <returns></returns>
        public List<TestItem> GetAllTestItemList()
        {
            var models = this.CurrentDBSession.TestItemDal.LoadEntitiesAsNoTracking(m => true).OrderBy(m => m.TestItemID).ToList();
            return models;
        }

        ///// <summary>
        ///// 获取样品类型List
        ///// </summary>
        ///// <returns></returns>
        //public List<DictionaryDetails> GetGoodsTypeList()
        //{
        //    var models = this.CurrentDBSession.DictionaryDetailsDal.LoadEntitiesAsNoTracking(m => m.dictType.DictTypeKey == "GoodsType").ToList();
        //    return models;
        //}

        /// <summary>
        /// 获取全部客户List，包括已经删除的
        /// </summary>
        /// <returns></returns>
        public List<Customer> GetAllCustomerList()
        {
            var models = this.CurrentDBSession.CustomerDal.LoadEntitiesAsNoTracking(m => true).OrderBy(m=>m.CustomerID).ToList();
            return models;
        }

        /// <summary>
        /// 获取全部客户List，不包括已经删除的
        /// </summary>
        /// <returns></returns>
        public List<Customer> GetCustomerList()
        {
            var models = this.CurrentDBSession.CustomerDal.LoadEntitiesAsNoTracking(m => true).OrderBy(m=>m.CustomerID).ToList();
            return models;
        }


        /// <summary>
        /// 获取角色列表List
        /// </summary>
        /// <returns></returns>
        public List<Role> GetRoleList()
        {
            var models = this.CurrentDBSession.RoleDal.LoadEntitiesAsNoTracking(m => true).ToList();
            return models;

        }


        /// <summary>
        /// 根据样品查询共有多少条该样品的报告
        /// </summary>
        /// <param name="Good">样品entity</param>
        /// <returns>报告数量</returns>
        public int GetReportCountByGoodsID(Good Good)
        {
            GoodsType GoodsType = this.CurrentDBSession.GoodsTypeDal.LoadEntitiesAsNoTracking(m => m.GoodsTypeID == Good.GoodsTypeID).First();
            if (GoodsType.GoodsTypeName == "原材料")
            {
                int count = this.CurrentDBSession.ReportRawDal.LoadEntitiesAsNoTracking(m => m.GoodsID == Good.GoodsID).Count();
                return count;
            }
            else if (GoodsType.GoodsTypeName == "产品")
            {
                int count = this.CurrentDBSession.ReportProductDal.LoadEntitiesAsNoTracking(m => m.GoodsID == Good.GoodsID).Count();
                count += this.CurrentDBSession.ReportSemiDal.LoadEntitiesAsNoTracking(m => m.GoodsID == Good.GoodsID).Count();
                return count;
            }
            else
            {
                return -1;
            }
        }

        /// <summary>
        /// 根据检验项目查询包含该检验项目的记录条数
        /// </summary>
        /// <param name="testItem">检验项目</param>
        /// <returns>记录条数</returns>
        public int GetReportCountByTestItem(TestItem testItem)
        {
            int count = 0;
            count += this.CurrentDBSession.ReportRawResultDal.LoadEntitiesAsNoTracking(m => m.TestItemID == testItem.TestItemID).Count();
            count += this.CurrentDBSession.ReportSemiResultDal.LoadEntitiesAsNoTracking(m => m.TestItemID == testItem.TestItemID).Count();
            count += this.CurrentDBSession.ReportProductResultDal.LoadEntitiesAsNoTracking(m => m.TestItemID == testItem.TestItemID).Count();
            return count;
        }

        /// <summary>
        /// 当系统设置-初始项目增加的时候调用此方法，新增系统中不存在的标准！
        /// </summary>
        /// <param name="reportType">报告类型</param>
        /// <returns>是否成功</returns>
        public int InitStandardWhenInitTestItemChanged(SysReportType reportType)
        {
            using (OJCB4Entities db = new OJCB4Entities())
            {
                if (reportType.SysReportTypeID == "1")  //原材料报告
                {
                    List<Good> GoodList = this.GetRawGoodList();
                    List<StandardRaw> standardList = new List<StandardRaw>();
                    foreach (var testItem in reportType.TestItems)
                    {
                        foreach (var Good in GoodList)
                        {
                            int count = this.CurrentDBSession.StandardRawDal.LoadEntitiesAsNoTracking(m => m.GoodsID == Good.GoodsID && m.TestItemID == testItem.TestItemID).Count();
                            if (count > 0) //说明已经存在
                            {
                                continue;
                            }
                            else
                            {
                                StandardRaw standard = new StandardRaw();
                                standard.FormOrder = testItem.Order;
                                standard.GoodsID = Good.GoodsID;
                                standard.TestItemID = testItem.TestItemID;
                                standard.MaxValue = "";
                                standard.MinValue = "";
                                standard.StandardRawID = Guid.NewGuid().ToString();
                                standardList.Add(standard);
                            }
                        }
                    }
                    foreach (var item in standardList)
                    {
                        db.Set<StandardRaw>().Add(item);
                    }
                }
                else if (reportType.SysReportTypeID == "2")  //过程报告
                {
                    List<Good> GoodList = this.GetSemiGoodList();
                    List<SamplingPlace> placeList = this.CurrentDBSession.SamplingPlaceDal.LoadEntitiesAsNoTracking(m => true).ToList();
                    foreach (var testItem in reportType.TestItems)
                    {
                        foreach (var place in placeList)
                        {
                            foreach (var Good in GoodList)
                            {
                                int count = this.CurrentDBSession.StandardSemiDal.LoadEntitiesAsNoTracking(m => m.GoodsID == Good.GoodsID && m.SamplingPlaceID == place.SamplingPlaceID && m.TestItemID == testItem.TestItemID).Count();
                                if (count > 0) //说明已经存在
                                {
                                    continue;
                                }
                                else
                                {
                                    StandardSemi standard = new StandardSemi();
                                    standard.SamplingPlaceID = place.SamplingPlaceID;
                                    standard.FormOrder = testItem.Order;
                                    standard.GoodsID = Good.GoodsID;
                                    standard.TestItemID = testItem.TestItemID;
                                    standard.StandardSemiID = Guid.NewGuid().ToString();
                                    db.Set<StandardSemi>().Add(standard);
                                }
                            }
                        }
                    }
                }
                else if (reportType.SysReportTypeID == "3")
                {
                    List<Customer> customerList = this.CurrentDBSession.CustomerDal.LoadEntitiesAsNoTracking(m => true).ToList();
                    List<Good> GoodList = this.GetSemiGoodList();
                    foreach (var testItem in reportType.TestItems)
                    {
                        //遍历所有客户 增加标准
                        foreach (var customer in customerList)
                        {
                            foreach (var Good in GoodList)
                            {
                                int count = this.CurrentDBSession.StandardCustomerDal.LoadEntitiesAsNoTracking(m => m.GoodsID == Good.GoodsID && m.CustomerID == customer.CustomerID && m.TestItemID == testItem.TestItemID).Count();
                                if (count > 0) //说明已经存在
                                {
                                    continue;
                                }
                                else
                                {
                                    StandardCustomer standard = new StandardCustomer();
                                    standard.CustomerID = customer.CustomerID;
                                    standard.FormOrder = testItem.Order;
                                    standard.GoodsID = Good.GoodsID;
                                    standard.TestItemID = testItem.TestItemID;
                                    standard.StandardCustomerID = Guid.NewGuid().ToString();
                                    db.Set<StandardCustomer>().Add(standard);
                                }
                            }
                        }
                    }
                }
                return db.SaveChanges();
            }
        }



        /// <summary>
        /// 获取报告类型列表
        /// </summary>
        /// <returns></returns>
        public List<SysReportType> GetReportTypeList()
        {
            return this.CurrentDBSession.SysReportTypeDal.LoadEntitiesAsNoTracking(m => true).ToList();
        }


        /// <summary>
        /// 获得检验项目顺序的最大值
        /// </summary>
        /// <returns></returns>
        public int GetMaxTestItemOrder()
        {
            var testItemList = this.CurrentDBSession.TestItemDal.LoadEntitiesAsNoTracking(m => true).ToList();
            int maxOrder = testItemList.Max(m => m.Order);
            return maxOrder;
        }


        /// <summary>
        /// 获取生产线List
        /// </summary>
        /// <returns></returns>
        public List<ProductionLine> GetProductionLineList()
        {
            var models = this.CurrentDBSession.ProductionLineDal.LoadEntitiesAsNoTracking(m => true).OrderBy(m => m.ProductionLineID).ToList();
            return models;
        }

        /// <summary>
        /// 获取班组列表
        /// </summary>
        /// <returns></returns>
        public List<ProductionGroup> GetProductionGropList()
        {
            var models = this.CurrentDBSession.ProductionGroupDal.LoadEntitiesAsNoTracking(m => true).OrderBy(m => m.ProductionGroupID).ToList();
            return models;
        }


        /// <summary>
        /// 获取成品报告的取样位置List
        /// </summary>
        /// <returns></returns>
        public List<SamplingPlaceMix> GetMixSamplingPlaceList()
        {
            return this.CurrentDBSession.SamplingPlaceMixDal.LoadEntitiesAsNoTracking(m => true).ToList();
        }

    }
}
